package com.example.sth0409.tablayoutdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.sth0409.tablayoutdemo.R;

import java.util.ArrayList;
import java.util.List;

public class ImageShowActivity extends FragmentActivity {

    private ViewPager activity_img_show_view_pager;
    private MyAdapter mAdapter;
    private StringBean stringBean;
    private int p;

    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    private List<String> stringList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);
        stringList = new ArrayList<>();
        initDate();
        initView();
        mAdapter = new MyAdapter(getSupportFragmentManager(), stringList);
        activity_img_show_view_pager.setAdapter(mAdapter);
        activity_img_show_view_pager.setCurrentItem(p);
    }

    private void initDate() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            stringBean = intent.getParcelableExtra("bean");
            stringList.addAll(stringBean.getStringList());
            p = intent.getIntExtra("position", -1);
        }
    }

    private void initView() {
        activity_img_show_view_pager = (ViewPager) findViewById(R.id.activity_img_show_view_pager);
    }


    static class MyAdapter extends FragmentStatePagerAdapter {
        private List<String> strings;

        public MyAdapter(FragmentManager fm, List<String> strings) {
            super(fm);
            this.strings = strings;
        }

        @Override
        public int getCount() {
            return strings.size();
        }

        //得到每个item
        @Override
        public Fragment getItem(int position) {

            return ArrayFragment.newInstance(position);
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            System.out.println("position Destory" + position);
            super.destroyItem(container, position, object);
        }

    }


    /**
     * 所有的 每个Fragment
     */
    public static class ArrayFragment extends Fragment {

        int mNum;

        static ArrayFragment newInstance(int num) {
            ArrayFragment array = new ArrayFragment();
            Bundle args = new Bundle();
            args.putInt("num", num);
            array.setArguments(args);
            return array;
        }


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
            System.out.println("mNum Fragment create =" + mNum);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            System.out.println("onCreateView = ");
            //在这里加载每个 fragment的显示的 View
            View v = null;
            List<String> strings = ((ImageShowActivity) getActivity()).getStringList();
            v = inflater.inflate(R.layout.image_show_frag, container, false);
            ImageView imageView = (ImageView) v.findViewById(R.id.iv);
            Glide.with(getContext()).load(strings.get(mNum)).fitCenter().into(imageView);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            System.out.println("onActivityCreated = ");
            super.onActivityCreated(savedInstanceState);
        }

        @Override
        public void onDestroyView() {
            System.out.println(mNum + "mNumDestory");
            super.onDestroyView();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }

    }
}
